package com.dk.controller;

import com.dk.pojo.RRole;
import com.dk.service.RRoleServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("rRoleController")
public class RRoleController {

    @Autowired
    private RRoleServiceI rRoleServiceI;

    //跳转角色页面
    @RequestMapping("list")
    public String listGet(){
        return "role/list";
    }

    //查询角色
    @RequestMapping("getInfoServer")
    @ResponseBody
    public List<RRole> getInfoServer(){
        List<RRole> list = rRoleServiceI.getInfoServer();
        return list;
    }

    //跳转增加角色及权限页面
    @RequestMapping("insertInfo")
    public String insertInfo(){
        return "role/add";
    }

    //确认增加
    @RequestMapping("saveById")
    @ResponseBody
    public  int saveById(String rname, @RequestParam("nodeIds[]")Integer[] nodeIds){
        rRoleServiceI.saveById(rname,nodeIds);
        return 1;
    }

    //根据角色id去查询角色数据回显
    @RequestMapping("updateInfo/{rid}")
    public String updateInfo(@PathVariable("rid")Integer rid, Model model){

        //角色查询
        RRole rRole = rRoleServiceI.getById(rid);
        model.addAttribute("r",rRole);
        return "role/update";
    }

    //确认修改角色加权限
    @RequestMapping("updateById")
    @ResponseBody
    public int updateById(RRole rRole, @RequestParam("nodeIds[]")Integer[] nodeIds){
        rRoleServiceI.updateById(rRole,nodeIds);
        return 1;
    }

    //删除
    @RequestMapping("deleteById/{rid}")
    @ResponseBody
    public int deleteById(@PathVariable("rid")Integer rid){
        rRoleServiceI.deleteById(rid);
        return 1;
    }
}
